﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Common;
using System.Threading;

namespace AnesSystem
{
    public class MongoDBUtil
    {
        #region Variables

        private int tipSpan = 1000 * 2;
        private bool taskStatus = true;
        private static MongoDBUtil _instance;

        #endregion

        #region Constructor

        public MongoDBUtil()
        {
            ConnectionStatus = true;
        }

        #endregion

        #region Properties

        public bool ConnectionStatus
        {
            get;set;
        }

        public static MongoDBUtil Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new MongoDBUtil();
                    _instance.StartTask();
                }
                return _instance;
            }
        }

        #endregion

        #region 依赖属性

        #endregion

        #region Event

        public event EventHandler<MongoConnectionArgs> ConnectionChanged;

        #endregion

        #region Private Methods

        #endregion

        #region Public/Protected Methods

        public void StartTask()
        {
            Task.Factory.StartNew(delegate
            {
                while (taskStatus)
                {
                    if (!HelperManager.Instance.MongoDB.ConnectionStatus)
                    {
                        ConnectionStatus = false;
                    }
                    else
                    {
                        ConnectionStatus = true;
                    }
                    ConnectionChanged?.Invoke(this, new MongoConnectionArgs(ConnectionStatus));
                    Thread.Sleep(tipSpan);
                }
            });
        }

        #endregion

        #region EventHandlers

        #endregion


    }
}
